package tankgame4;

/**
 * 1 .在敌人坦克类，使用Vector里保存多个Shot
 * 2 .当每创建一个敌人坦克对豢，给该敌人坦克对象初始
 * 化一个Shot对象，同时启动Shot
 * 3 .在绘制敌人坦克时，需要变量敌人坦克对象Vector，绘
 * 制所有的子弹,当子弹isLive == false时，就从Vector移
 * 除
 */

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();
    boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }

    @Override
    public void run() {
        while (true) {
            //如果shots.size = 0,创建一颗子弹放入shots集合
            if (isLive && shots.size() == 0) {
                Shot s = null;
                switch (getDirect()) {
                    case 0:
                        s = new Shot(getX() + 20, getY(), 0);
                        break;
                    case 1:
                        s = new Shot(getX() + 60, getY() + 20, 1);
                        break;
                    case 2:
                        s = new Shot(getX() + 20, getY() + 60, 2);
                        break;
                    case 3:
                        s = new Shot(getX(), getY() + 20, 3);
                        break;
                }
                shots.add(s);
                new Thread(s).start();
            }

                //根据坦克的方向继续移动
                switch (getDirect()) {
                    case 0://让坦克保持一个方向走30步
                        for (int i = 0; i < 30; i++) {
                            if (getY() > 0) {
                                moveUp();
                            }
                            //休眠50毫秒
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        break;
                    case 1:
                        for (int i = 0; i < 30; i++) {
                            if (getX() + 60 < 1000) {
                                moveRight();
                            }
                            //休眠50毫秒
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        break;

                    case 2:
                        for (int i = 0; i < 30; i++) {
                            if (getY() + 60 < 750) {
                                moveDown();
                            }
                            //休眠50毫秒
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        break;

                    case 3:
                        for (int i = 0; i < 30; i++) {
                            if (getX() > 0) {
                                moveLeft();
                            }
                            //休眠50毫秒
                            try {
                                Thread.sleep(50);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                        break;

                }
                //随机改变坦克运动方向
                setDirect((int) (Math.random() * 4));
                if (!isLive) {
                    break;//退出线程
                }


        }
    }
}
